<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

include('includes/Admin.Core.php');

CheckAccess('managenews');


PrintHeader('新闻管理');

$action = ForceIncomingString('action', 'displaynews');
if(IsPost('updatenews')) $action= 'updatenews';
if(IsPost('deletenews')) $action= 'deletenews';

// ############################## FUNCTIONS #############################

function GetSearchSql($search, $actived) {
	$sWhere = '';
	if($search != ''){
		$sWhere .= " WHERE (username LIKE '%".$search."%' OR keywords LIKE '%".$search."%' OR title LIKE '%".$search."%' OR content LIKE '%".$search."%') ";

		if($actived == '1'){
			$sWhere .= " AND actived = '1' ";
		}elseif($actived == '-1'){
			$sWhere .= " AND actived = '0' ";
		}
	}elseif($actived == '1'){
		$sWhere .= " WHERE actived = '1' ";
	}elseif($actived == '-1' ){
		$sWhere .= " WHERE actived = '0' ";
	}

	return $sWhere;
}

// ################################ INSERT NEW ################################

if($action == 'insertnew' OR $action == 'updatenew')
{
	$newid     = ForceIncomingInt('newid');
	$ordernum     = ForceIncomingInt('ordernum');
	$username        = ForceIncomingString('username', $userinfo['username']);
	$title        = ForceIncomingString('title');
	$content        = ForceIncomingString('content');
	$keywords        = ForceIncomingString('keywords');
	$actived     = ForceIncomingInt('actived');
	$viewcount     = ForceIncomingInt('viewcount');
	$deletenew     = ForceIncomingInt('deletenew');

	if ($deletenew)
	{
		$DB->query("DELETE FROM " . TABLE_PREFIX . "news WHERE newid = '$newid' ");
		PrintRedirect('admin.news.php', 1);
	}

	if (strlen($title) == 0)
	{
		$errors[] = "新闻标题不能为空!";
	}

	if(strlen($content) == 0){
		$errors[] = "新闻内容不能为空!";
	}

	if ($action == 'insertnew' AND !isset($errors))
	{
		$DB->query("INSERT INTO " . TABLE_PREFIX . "news VALUES (NULL, '$ordernum', '$actived', '$username', '$title', '$content', '$keywords', '0', '".time()."')");

		$newid = $DB->insert_id();
		$DB->query("UPDATE " . TABLE_PREFIX . "news SET ordernum = '$newid' WHERE newid = '$newid'");

		PrintRedirect('admin.news.php', 1);

	}elseif($action == 'updatenew' AND !isset($errors)){
		$DB->query("UPDATE " . TABLE_PREFIX . "news SET 
		ordernum     = '$ordernum',
		actived     = '$actived',
		username = '$username',
		title     = '$title',
		content     = '$content',
		keywords     = '$keywords',
		viewcount     = '$viewcount'
		WHERE newid   = '$newid'");

		PrintRedirect('admin.news.php', 1);
	}else{
		PrintErrors($errors, Iif($newid, '编辑新闻错误', '添加新闻错误'));
		$action = 'addnew';
	}

}


// ############################### UPDATE NEWS ################################


if($action == 'updatenews'){
	$newids   = ForceIncomingArray('updatenewids');
	$ordernums   = ForceIncomingArray('ordernums');
	$activeds   = ForceIncomingArray('activeds');

    for($i = 0; $i < count($newids); $i++)
    {
		$DB->query("UPDATE " . TABLE_PREFIX . "news SET ordernum = '". ForceInt($ordernums[$i])."',
		actived = '$activeds[$i]'
		WHERE newid = '$newids[$i]'");
    }

    PrintRedirect('admin.news.php', 1);

}


// ############################### DELETE NEWS ################################

if($action == 'deletenews')
{
	$deletenewids = ForceIncomingArray('deletenewids');

	for($i = 0; $i < count($deletenewids); $i++){
		$DB->query("DELETE FROM " . TABLE_PREFIX . "news WHERE newid = '$deletenewids[$i]' ");
	}

	PrintRedirect('admin.news.php', 1);
}

// ############################ DISPLAY NEW FORUM #############################

if($action == 'addnew' OR $action == 'editnew')
{
	$newid = ForceIncomingInt('newid');

	if(isset($errors))
	{
		// new new or edit new with errors, redisplaying the form
		$new = array('newid'   => $newid,
			  'ordernum'     => $ordernum,
			  'username'     => $username,
			  'title'     => $title,
			  'content'     => $_POST['content'],
			  'keywords'     => $keywords,
			  'viewcount'     => $viewcount,
			  'actived'     => $actived);

	} elseif($newid) {
		$new = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "news WHERE newid = '$newid'");
	}else{
		$new = array('newid' => 0, 'actived' => 1, 'username' => $userinfo['username']);
	}

	if($newid)
	{
		PrintSection('编辑新闻: ' . ShortTitle($new['title'], 36));
	}else{
		PrintSection('添加新闻');
	}

	echo '<script src="'.BASEURL.'library/editor/nicEdit.js" type="text/javascript"></script>
	<script type="text/javascript">
	bkLib.onDomLoaded(function() {
		new nicEditor({fullPanel:true, iconsPath: \''.BASEURL.'library/editor/nicEditorIcons.gif\', maxHeight:400}).panelInstance("content");
	});
	</script>';

	echo '<form method="post" action="admin.news.php">
	<input type="hidden" name="action" value="' . Iif($newid, 'updatenew', 'insertnew') . '" />
	<input type="hidden" name="newid" value="' . $new['newid'] . '" />
	<table width="100%" border="0" cellpadding="5" cellspacing="0">';

	if($newid)
	{
		echo '<tr>
		<td class="tdrow2" nowrap="nowrap"><b>编号:</b></td>
		<td class="tdrow3"><input type="text" name="ordernum" value="'.$new['ordernum'].'" size="16" /></td>
		</tr>';
	}

	echo '<tr>
	<td class="tdrow2" nowrap="nowrap"><b>发表人:</b></td>
	<td class="tdrow3"><input type="text" name="username" value="'.$new['username'].'" size="16" /></td>
	</tr>
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>新闻标题:</b></td>
	<td class="tdrow3"><input type="text" name="title" value="'.$new['title'].'" size="40" /></td>
	</tr>
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>新闻内容:</b></td>
	<td class="tdrow3" style="background: #fff;"><textarea name="content" style="height:390px;width:600px;" id="content">'.$new['content'].'</textarea></td>
	</tr>	
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>Meta关键字:</b></td>
	<td class="tdrow3"><input type="text" name="keywords" value="'.$new['keywords'].'" size="60" /></td>
	</tr>';

	if($newid)
	{
		echo '<tr>
		<td class="tdrow2" nowrap="nowrap"><b>点击次数:</b></td>
		<td class="tdrow3"><input type="text" name="viewcount" value="'.$new['viewcount'].'" size="16" /></td>
		</tr>';
	}

	echo '<tr>
	<td class="tdrow2" nowrap="nowrap"><b>是否发布:</b></td>
	<td class="tdrow3"><input type="checkbox" name="actived" value="1" '.Iif($new['actived'] == 1, 'CHECKED').'> <b>是:</b> 是否发布此新闻?</td>
	</tr>';

	if($newid)
	{
		echo '<tr>
		<td class="tdrow2" nowrap="nowrap"><b>是否删除:</b></td>
		<td class="tdrow3"><input type="checkbox" name="deletenew" value="1"> <b>是:</b> <span class=ohred>是否删除此新闻?</span></td>
		</tr>';
	}

	echo '<tr>
	<td class="tdrow1" bgcolor="#FCFCFC" colspan="2" align="center">
	<input type="submit" value="' . Iif($newid, '保存更新', '提交保存') . '" />
	</td>
	</tr>
	</table>
	</form>';

	EndSection();
}


// ############################### DISPLAY NEWS ###############################

if($action == 'displaynews')
{
	$NumPerPage =20;

	$page = ForceIncomingInt('page', 1);
	$search = ForceIncomingString('s');
	$actived = ForceIncomingInt('a');

	if(IsGet('s')){
		$search = urldecode($search);
	}

	$start = $NumPerPage * ($page-1);

	PrintSection('搜索新闻');
	echo '<form method="post" action="admin.news.php" name="searchnews">
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow2" align="center">搜索:&nbsp;<input type="text" name="s" size="22">&nbsp;&nbsp;选择:&nbsp;<select name="a">
	<option value="0">全部新闻</option>
	<option style="color:red;" value="-1">未发布的新闻</option>
	<option value="1">已发布的新闻</option>
	</select>&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="searchnew" value="搜索新闻" /></td>
	</tr>
	</table></form>';
	EndSection();

	$title = Iif(IsPost('searchnew'), '搜索到的新闻列表', '全部新闻列表');

	$getnews = $DB->query("SELECT newid, ordernum, actived, username, title, viewcount, created FROM " . TABLE_PREFIX . "news ".GetSearchSql($search, $actived)." ORDER BY ordernum DESC LIMIT $start,$NumPerPage");

	$maxrows = $DB->query_first("SELECT COUNT(newid) AS value FROM " . TABLE_PREFIX . "news ".GetSearchSql($search, $actived));

	echo '&nbsp;&nbsp;&nbsp;&nbsp;<a href="admin.news.php?action=addnew"><img src="'.TEMPLATEPATH.'images/open.gif" align="absmiddle"> <font class=ohblueb><u>添加新闻</u></font></a>';

	PrintSection($title.'('.$maxrows['value'].'个)');

	echo '<form method="post" action="admin.news.php" name="newsform">
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" nowrap="nowrap">编号</td>
	<td class="tdrow1" nowrap="nowrap">标题</td>
	<td class="tdrow1" nowrap="nowrap">状态</td>
	<td class="tdrow1" nowrap="nowrap">发表人</td>
	<td class="tdrow1" nowrap="nowrap">点击</td>
	<td class="tdrow1" nowrap="nowrap">日期</td>
	<td class="tdrow1" nowrap="nowrap"><input type="checkbox" checkall="group" onclick="select_deselectAll (\'newsform\', this, \'group\');"> 删除</td>
	</tr>';

	if($maxrows['value'] < 1){
		echo '<tr><td class="tdrow3" colspan="10" align="center" height="48" valign="middle"><font class=ohredb>未搜索到任何新闻!</font></td></tr>';
	}else{
		while($new = $DB->getrow($getnews))
		{
			echo '<tr>
			<td class="tdrow2"><input type="hidden" name="updatenewids[]" value="'.$new['newid'].'" /><input type="text" name="ordernums[]" value="' . $new['ordernum'] . '" size="4" /></td>
			<td class="tdrow3"><a href="admin.news.php?action=editnew&newid='.$new['newid'].'"  title="'.$new['title'].'">'.Iif($new['actived'] != '1','<font class=ohred><u>').ShortTitle($new['title'], 48).Iif($new['actived'] != '1','</u></font>').'</a></td>
			<td class="tdrow2"><select name="activeds[]">
			<option value="1">发布</option>
			<option style="color:red;" value="0" ' . Iif(!$new['actived'], 'SELECTED', '') . '>隐藏</option>
			</select></td>
			<td class="tdrow3">' . $new['username'] . '</td>
			<td class="tdrow2">' . $new['viewcount'] . '</td>
			<td class="tdrow3" nowrap="nowrap">' . DisplayDate($new['created'], 'Y-m-d') . '</td>
			<td class="tdrow2"><input type="checkbox" name="deletenewids[]" value="' . $new['newid'] . '" checkme="group" /></td>
			</tr>';
		}
	}

	$totalpages = ceil($maxrows['value'] / $NumPerPage);

	if($totalpages > 1){
		echo '<tr><td class="tdrow3" colspan="7" align="center" height="48" valign="middle">';
		echo GetPageList('admin.news.php', $totalpages, $page, 10, 's', urlencode($search), 'a', $actived);
		echo '</td></tr>';
	}

	if($maxrows['value'] > 0){
		echo '<tr><td class="tdrow1" colspan="7" align="center"><input type="submit" name="updatenews" value=" 保存更新 " />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="deletenews" onclick="return confirm(\'确定删除所选新闻吗?\');" value=" 删除新闻 " /></td></tr>';
	}

	echo '</table></form>';

	EndSection();
}


// ############################### PRINT FOOTER ################################

PrintFooter();

?>